<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_mutasi_toko extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_mutasi_toko_model', 'receive');
		$this->load->model('kst_dt_mutasi_toko_model', 'Detail');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Mutasi Toko";

		viewByTemplate('kst_mutasi_toko/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->receive->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->receive->GetJSON2();
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('receive_toko_id'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Mutasi Toko";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";

		$data['combopetugas'] = $this->receive->petugas();
		$data['combocabang'] = $this->receive->cabang();

		$query = $this->db->query("SELECT receive_toko_no FROM (SELECT * FROM kst_order_receive_toko2 ORDER BY receive_toko_no DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['receive_toko_no'];
		else
			$MaxID = "OR0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['receive_toko_auto'] = "OR" . sprintf("%07s",$NextID);
		
		viewByTemplate('kst_mutasi_toko/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Mutasi Toko";
		$data['data'] = $this->receive->GetSingleView(get('receive_toko_id'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		
		$data['combopetugas'] = $this->receive->petugas();
		$data['combocabang'] = $this->receive->cabang();

		$query = $this->db->query("SELECT receive_toko_no FROM (SELECT * FROM kst_order_receive_toko2 where RECEIVE_TOKO_ID = ".$this->input->get('receive_toko_id')." ORDER BY receive_toko_no DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['receive_toko_no'];
		else
			$MaxID = "OR0000000";

		viewByTemplate('kst_mutasi_toko/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Mutasi Toko";
		$data['data'] = $this->receive->GetSingleView(get('receive_toko_id'));
		
		$data['mode'] = "view";

		$data['combopetugas'] = $this->receive->petugas();
		$data['combocabang'] = $this->receive->cabang();

		viewByTemplate('kst_mutasi_toko/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("receive_toko_no") == '') {
			$error->Add("receive_toko_no", "No Permintaan", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM kst_order_receive_toko2 WHERE receive_toko_no = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("receive_toko_no"))))."'")) > 0) {
			$error->Add("receive_toko_no", "No Permintaan", $tpl_duplicate);
		}else if (post("supplier_nama") == '') {
			$error->Add("supplier_nama", "Petugas", $tpl_empty);
		}else if (post("receive_toko_cabang") == '') {
			$error->Add("receive_toko_cabang", "Cabang", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// // Fill Post Data
				// $query = $this->db->query("SELECT receive_toko_id FROM (SELECT * FROM kst_order_receive_toko2 ORDER BY receive_toko_id DESC) WHERE rownum <= 1");
				// $getquery = $query->row_array();
				// $rowcount = $getquery['receive_toko_id'];
				// $autonum = $rowcount + 1;

				if (post("mode") == "create")
				{
					$query = $this->db->query("SELECT receive_toko_id FROM (SELECT * FROM kst_order_receive_toko2 ORDER BY receive_toko_no DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['receive_toko_id'];
						$autonum = $rowcount + 1;
					} else {
						$autonum = 1;
					}
				}
				else if (post("mode") == "edit")
				{
					$query = $this->db->query("SELECT receive_toko_id FROM (SELECT * FROM kst_order_receive_toko2 where receive_toko_no = '".$this->input->post('receive_toko_no')."' ORDER BY receive_toko_no DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['receive_toko_id'];
						$autonum = $rowcount;
					} else {
						$autonum = 1;
					}
				}

				$receive_toko_id_x = $autonum;

				$tgl_old = str_replace(" ", "", $this->input->post('receive_toko_tgl'));

				$tahun_new = substr($tgl_old, 6, 4);
			    $bulan_new = substr($tgl_old, 3, 2);
			    $hari_new   = substr($tgl_old, 0, 2);

			    $tgl_new = $tahun_new."-".$bulan_new."-".$hari_new;

				$data = array(
								'receive_toko_id' => $autonum,
								'receive_toko_cabang' => $this->input->post('receive_toko_cabang'),
								'receive_toko_no' => $this->input->post('receive_toko_no'),
								'supplier_nama' => $this->input->post('supplier_nama'),
								'receive_toko_status' => $this->input->post('receive_toko_status'),
								'receive_toko_tgl' => $tgl_new,
								'receive_toko_gudang' => $this->input->post('receive_toko_gudang'),		
							  );
		

				// $data['receive_toko_no']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['receive_toko_no'])));
				// $data['receive_toko_id']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['receive_toko_id'])));
				
				if (post("mode") == "create")
				{
					$this->receive->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->receive->Update($data, array('receive_toko_id'	=> $data['receive_toko_id']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}


			
			// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->Detail->DeleteSingle($row['receive_toko_dt_id']);
				}
				
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					
					// Auto number kode detail receive
					
					
					$query = $this->db->query("SELECT receive_toko_dt_id FROM (SELECT * FROM kst_order_receive_toko_dt2 ORDER BY receive_toko_dt_id DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['receive_toko_dt_id'];
						$autonum = $rowcount + 1;
					} else {
						$autonum = 1;
					}

					$row = $dtl_sample2['inserted'][$i];
					$row['receive_toko_dt_id'] = $autonum;
					$row['receive_toko_id'] = $receive_toko_id_x;
					$row['receive_toko_dt_barang'] = $row['receive_toko_dt_barang'];
					$row['receive_toko_dt_barcode'] = $row['receive_toko_dt_barcode'];
					$row['receive_toko_dt_kemasan'] = $row['receive_toko_dt_kemasan'];
					$row['receive_toko_dt_ckemasan'] = $row['receive_toko_dt_ckemasan'];
					$row['receive_toko_dt_cckemasan'] = $row['receive_toko_dt_cckemasan'];
					$row['receive_toko_dt_pesan'] = $row['receive_toko_dt_pesan'];
					$row['receive_toko_dt_kirim'] = $row['receive_toko_dt_kirim'];
					$row['receive_toko_dt_terima'] = $row['receive_toko_dt_terima'];
					$row['receive_toko_dt_hpokok'] = $row['receive_toko_dt_hpokok'];
					$row['receive_toko_dt_hjual'] = $row['receive_toko_dt_hjual'];
					$row['receive_toko_dt_diskon'] = $row['receive_toko_dt_diskon'];
					$row['receive_toko_dt_ppn'] = $row['receive_toko_dt_ppn'];
					$row['receive_toko_dt_terima2'] = $row['receive_toko_dt_terima2'];
					$row['receive_toko_dt_catatan'] = $row['receive_toko_dt_catatan'];
					$this->Detail->Add($row);
				}
				
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];					
					$receive_toko_dt_id	= $row['receive_toko_dt_id'];
					unset($row['receive_toko_dt_id']);					
					//$row['jumlah'] = (float)$row['jumlah'];					
					$this->Detail->Update($row, array('receive_toko_dt_id'	=> $receive_toko_dt_id));
				}
			
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}




	function ajax_delete()
	{
		$ls_result	= "";
		$receive_toko_id = get('receive_toko_id');
		
		if ($receive_toko_id != "")
		{
			// Delete Single
			$arr_rule	= array('receive_toko_id' => $receive_toko_id);
			$ls_result = $this->receive->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('receive_toko_id' => $row['receive_toko_id']);
				$ls_result .= $this->receive->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>